package myJDBC.entities;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import constants.AttributeConstants;
import constants.ListValueConstants;
import constants.ObjectTypeConstants;
import myJDBC.myConnection;
import myJDBC.lowLevel.Objects;
import myJDBC.lowLevel.Params;
import myJDBC.lowLevel.References;

public class Orders {

	public static String retTaskCustomerFIO(int taskId){
		int ordId=Integer.parseInt(Objects.returnObjInfo(taskId).get(3));
		int custId=Integer.parseInt(Objects.returnObjInfo(ordId).get(3));
		return Params.returnVarchNumb(AttributeConstants.FIO, custId);
	}
	
	public static String retTaskCustomerAdress(int taskId){
		String ret="";
		int ordId=Integer.parseInt(Objects.returnObjInfo(taskId).get(3));
		int custId=Integer.parseInt(Objects.returnObjInfo(ordId).get(3));
		try {
			ret=References.returnNameObj(AttributeConstants.ref_country, custId).get(1);ret+="/";
			ret+=References.returnNameObj(AttributeConstants.ref_city, custId).get(1);ret+="/";
			ret+=References.returnNameObj(AttributeConstants.ref_district, custId).get(1);ret+="/";
			ret+=References.returnNameObj(AttributeConstants.ref_street, custId).get(1);ret+="/";
			ret+=References.returnNameObj(AttributeConstants.ref_house, custId).get(1);ret+="/";
			ret+=References.returnNameObj(AttributeConstants.ref_porch, custId).get(1);ret+="/";
			ret+=References.returnNameObj(AttributeConstants.ref_apartment, custId).get(1);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return ret;
	}
	
	public static ArrayList<String> retOrderInfo(int taskId){
		ArrayList<String> ret = new ArrayList<String>();
		int ordId=Integer.parseInt(Objects.returnObjInfo(taskId).get(3));
		int custId=Integer.parseInt(Objects.returnObjInfo(ordId).get(3));
		try {
			ret.add(String.valueOf(References.returnNameObj(AttributeConstants.ref_group_id, taskId).get(0)));
			ret.add(String.valueOf(ordId));
			ret.add(Objects.returnObjInfo(taskId).get(2));
			ret.add(Params.returnVarchNumb(AttributeConstants.FIO, custId));
			ret.add(References.returnNameObj(AttributeConstants.ref_country, custId).get(1));
			ret.add(References.returnNameObj(AttributeConstants.ref_city, custId).get(1));
			ret.add(References.returnNameObj(AttributeConstants.ref_district, custId).get(1));
			ret.add(References.returnNameObj(AttributeConstants.ref_street, custId).get(1));
			ret.add(References.returnNameObj(AttributeConstants.ref_house, custId).get(1));
			ret.add(References.returnNameObj(AttributeConstants.ref_porch, custId).get(1));
			ret.add(References.returnNameObj(AttributeConstants.ref_apartment, custId).get(1));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ret;
	}
	
	// ordID, name, description, циклично повторяет: attrid, value
	public static ArrayList<String> retManualTask(int objid) throws SQLException {
		ArrayList<String> ret = new ArrayList<String>();
		int manTaskId=ObjectTypeConstants.task_manual;
		ArrayList<String> tmp = new ArrayList<String>();
		tmp = Objects.returnObjInfo(objid);
		ret.add(String.valueOf(objid)); // id
		ret.add(tmp.get(0)); // name
		ret.add(tmp.get(1)); // descr
		ArrayList<ArrayList<String>> tmp2 = new ArrayList<ArrayList<String>>();
		tmp2 = Objects.returnObjectAttributesWithValIdNewIdonly(objid, manTaskId);
		for (ArrayList<String> al : tmp2) {
			ret.add(al.get(0));// attrid
			ret.add(al.get(1));// attrvalue
		}
		return ret;
	}

	public static ArrayList<ArrayList<String>> retMyManualTasks(int userId) throws SQLException{
		ArrayList<ArrayList<String>> ret = new ArrayList<ArrayList<String>>();
		ArrayList<Integer> al = new ArrayList<Integer>();
		int manTaskId=ObjectTypeConstants.task_manual;
		String select = "select \"object_id\" from \"objects\" where \"object_type_id\"="+manTaskId + "";
		if (myConnection.dbConnection != null) {
			try {
				Statement statement = myConnection.dbConnection
						.createStatement();
				ResultSet rs = statement.executeQuery(select);
				while (rs.next()) {
					al.add(rs.getInt(1));
				}
			} catch (SQLException e) {
				System.out.println(e.getMessage());
			}
		}
		for (Integer i : al) {
			ArrayList<String> tmp = retManualTask(i);
			if (checkMyOrNot(tmp, userId))
				ret.add(tmp);
		}
		return ret;
	}
	
	public static ArrayList<ArrayList<String>> retFreeManualTasksWithGroups(ArrayList<Integer> myGroups) throws SQLException {
		ArrayList<ArrayList<String>> ret = new ArrayList<ArrayList<String>>();
		ArrayList<Integer> al = new ArrayList<Integer>();
		//int objTypeId = ObjectTypes.returnObjTypeId("order");
		int manTaskId=ObjectTypeConstants.task_manual;
		String select = "select \"object_id\" from \"objects\" where \"object_type_id\"="
				+ manTaskId + "";
		if (myConnection.dbConnection != null) {
			try {
				Statement statement = myConnection.dbConnection
						.createStatement();
				ResultSet rs = statement.executeQuery(select);
				while (rs.next()) {
					al.add(rs.getInt(1));
				}
			} catch (SQLException e) {
				System.out.println(e.getMessage());
			}
		}
		for (Integer i : al) {
			ArrayList<String> tmp = retManualTask(i);
			if (checkFreeAndGroup(tmp, myGroups))
				ret.add(tmp);
		}
		return ret;
	}

	private static boolean checkFreeAndGroup(ArrayList<String> in, ArrayList<Integer> myGroups) {
		boolean gr = false;
		boolean fr = false;
		for (int i = 3; i < in.size(); i = i + 2) {
			if ((Integer.parseInt(in.get(i))==AttributeConstants.ref_group_id) && (myGroups.contains(Integer.parseInt(in.get(i + 1))))){
				gr = true;
			}			
			if ((Integer.parseInt(in.get(i))==AttributeConstants.manual_task_status) && (Integer.parseInt(in.get(i + 1))==ListValueConstants.manual_task_status_initiated)) {
				fr = true;
			}
		}
		if (gr && fr)
			return true;
		else
			return false;
	}
	
	private static boolean checkMyOrNot(ArrayList<String> in, int myId) {
		boolean gr = false;
		for (int i = 3; i < in.size(); i = i + 2) {
				if ((Integer.parseInt(in.get(i))==AttributeConstants.ref_user_id) && Integer.parseInt(in.get(i + 1))==myId) {
				gr = true;
			}
		}
		return gr;
	}

	public static void takeManualTask(int custId, int taskId) throws SQLException {
		References.createReference(AttributeConstants.ref_user_id, taskId, custId);
		Params.changeValueId(taskId, AttributeConstants.manual_task_status, ListValueConstants.manual_task_status_in_progress);
	}	
	
	public static void manualTaskDone(int manTaskId) throws SQLException {
		References.deleteReference(AttributeConstants.ref_user_id, manTaskId);
		Params.changeValueId(manTaskId,AttributeConstants.manual_task_status, ListValueConstants.manual_task_status_complete);
	}	
}
